package com.maxd.controller;

import com.maxd.adapter.LoginUserHolder;
import com.maxd.bean.PageRes;
import com.maxd.bean.classes.ClassDetail;
import com.maxd.bean.classes.LessonDetail;
import com.maxd.bean.classes.LessonSignReq;
import com.maxd.error.ExceptionEnum;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.ClassInfo;
import com.maxd.model.ClassLessonInfo;
import com.maxd.service.ClassService;
import com.maxd.utils.AdminUtils;
import com.maxd.utils.AssertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value="班级管理", tags = {"班级管理"})
@RequestMapping(value = "/class")
public class ClassController {
    
    @Autowired
    private ClassService classService;

    @PostMapping("/save-class")
    @ApiOperation("内管新增/修改班级信息")
    public Result<Void> classPage(@RequestBody @Validated ClassInfo classInfo) {
        AdminUtils.checkLogin();
        classService.save(classInfo);
        return ResultUtil.success(null);
    }

    @GetMapping("/class-page")
    @ApiOperation("内管分页查询班级列表")
    public Result<PageRes<ClassInfo>> classPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                    @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                    @ApiParam("班级名称，支持模糊查询") @RequestParam(value = "name", required = false) String name) {
        AdminUtils.checkLogin();
        PageRes<ClassInfo> pageRes = classService.queryPage(pageNo, pageSize, name, null);
        return ResultUtil.success(pageRes);
    }

    @GetMapping("/class-list")
    @ApiOperation("内管查询班级列表")
    public Result<List<ClassInfo>> classList(@ApiParam("班级名称，支持模糊查询") @RequestParam(value = "name", required = false) String name) {
        AdminUtils.checkLogin();
        List<ClassInfo> classInfos = classService.queryList(name, null);
        return ResultUtil.success(classInfos);
    }

    @GetMapping("/class-detail")
    @ApiOperation("查询班级详情")
    public Result<ClassDetail> classDetail(@ApiParam("班级ID") @RequestParam(value = "id") Long id) {
        AdminUtils.checkLogin();
        ClassDetail classDetail = classService.detail(id);
        return ResultUtil.success(classDetail);
    }

    @GetMapping("/my-class-list")
    @ApiOperation("教官查询班级列表(商城端)")
    public Result<List<ClassInfo>> myClassList() {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);

        List<ClassInfo> classInfos = classService.queryList(null, userId);
        return ResultUtil.success(classInfos);
    }

    @PostMapping("/add-class-lesson")
    @ApiOperation("教官新增/修改排课(商城端)")
    public Result<Void> addClassLesson(@RequestBody @Validated ClassLessonInfo classLessonInfo) {
        classService.addClassLesson(classLessonInfo);
        return ResultUtil.success(null);
    }

    @PostMapping("/sign-lesson")
    @ApiOperation("教官课程签到(商城端)")
    public Result<Void> signLesson(@RequestBody @Validated LessonSignReq req) {
        classService.signLesson(req);
        return ResultUtil.success(null);
    }

    @GetMapping("/my-class-lesson-page")
    @ApiOperation("教官查询班级排班信息(商城端)")
    public Result<PageRes<ClassLessonInfo>> myClassLessonPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                              @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize) {
        Long userId = LoginUserHolder.getUserId();
        AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        PageRes<ClassLessonInfo> pageInfo = classService.queryClassLessonPage(pageNo, pageSize, null, userId, null);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/class-lesson-page")
    @ApiOperation("查询班级排班信息(内管)")
    public Result<PageRes<ClassLessonInfo>> classLessonPage(@ApiParam("页码，从0页开始") @RequestParam("pageNo") Integer pageNo,
                                                            @ApiParam("每页记录数量") @RequestParam("pageSize") Integer pageSize,
                                                            @ApiParam("班级ID") @RequestParam(value = "classId", required = false) Long classId,
                                                            @ApiParam("教官用户ID") @RequestParam(value = "instructorUserId", required = false) Long instructorUserId) {
        AdminUtils.checkLogin();
        PageRes<ClassLessonInfo> pageInfo = classService.queryClassLessonPage(pageNo, pageSize, classId, instructorUserId, null);
        return ResultUtil.success(pageInfo);
    }

    @GetMapping("/class-lesson-detail")
    @ApiOperation("查询课程排班详情")
    public Result<LessonDetail> classLessonDetail(@ApiParam("课程ID") @RequestParam("id")  Long id) {
        Long userId = null;
        if(LoginUserHolder.getAdminId() == null) {
            userId = LoginUserHolder.getUserId();
            AssertUtil.assertTrue(userId != null && userId > 0, ExceptionEnum.WRONT_TOKEN);
        }
        LessonDetail lessonDetail = classService.queryClassLessonDetail(id, userId);
        return ResultUtil.success(lessonDetail);
    }

}
